#include "serverread.h"

#include <unistd.h>
#include <string.h>
#include <QDebug>

ServerRead::ServerRead(QObject *parent)
    : QObject{parent}
{
    m_runningFlag = true;
}

void ServerRead::reading(int clientFd)
{
    qDebug() << "serverRead: start read" << clientFd;
    char buffer[4096] = {0};
    while (m_runningFlag)
    {
        int size = ::read(clientFd, buffer, sizeof buffer);
        if (size == 0)
        {
            qDebug() << "服务器：套结字断开";
            break;
        }
        if (size == -1)
        {
            qDebug() << "服务器：客户端断开连接";
            break;
        }
        qDebug() << "serverRead: get" << size;
        QString msg = QString::fromUtf8(buffer);
        qDebug() << "msg:" << msg;
        emit showReceivedMsg(msg);

    }
}

void ServerRead::stopNow()
{
    m_runningFlag = false;
}
